package com.isias.system.service.Impl;

import com.isias.model.dto.CourseWithStar;
import com.isias.model.dto.HomeDto;
import com.isias.model.system.SysUser;
import com.isias.system.mapper.*;
import com.isias.system.service.HomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
@Transactional(rollbackFor = Exception.class)
public class HomeServiceImpl implements HomeService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private SysPostMapper sysPostMapper;

    @Autowired
    private HomeMapper homeMapper;

    @Override
    public HomeDto getInfo(String username) {
        HomeDto homeDto = new HomeDto();
        SysUser sysUser = sysUserMapper.selectUser(username);
        homeDto.setSysUser(sysUser);
        homeDto.setCourse(homeMapper.getCourse());
        homeDto.setSevenLoginNum(getSevenLogin());
        homeDto.setSevenLoginDate(getSevenLogindate());
        //根据user的postid和deptid和roleid来获取名字岗位和部门名称
        homeDto.setDeptname(sysDeptMapper.selectNameById(sysUser.getDeptId()));
        homeDto.setRoleName(sysPostMapper.selectNameById(sysUser.getPostId()));
        return homeDto;
    }

    public int [] getSevenLogin() {
        LocalDate now1 = LocalDate.now().plusDays(1);
        LocalDate now = LocalDate.now();
        LocalDate date6 = LocalDate.now().plusDays(-1);
        LocalDate date5 = LocalDate.now().plusDays(-2);
        LocalDate date4 = LocalDate.now().plusDays(-3);
        LocalDate date3 = LocalDate.now().plusDays(-4);
        LocalDate date2 = LocalDate.now().plusDays(-5);
        LocalDate date1 = LocalDate.now().plusDays(-6);

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String format1 = date1.format(formatter)+" 00:00:00";   //七天前的登录次数
        String format2 = date2.format(formatter)+" 00:00:00";
        String format3 = date3.format(formatter)+" 00:00:00";
        String format4 = date4.format(formatter)+" 00:00:00";
        String format5 = date5.format(formatter)+" 00:00:00";
        String format6 = date6.format(formatter)+" 00:00:00";
        String format7 = now.format(formatter)+" 00:00:00";
        String format8 = now1.format(formatter)+" 00:00:00";


        Integer integer1 = homeMapper.DayUsersLogin(format1, format2);
        Integer integer2 = homeMapper.DayUsersLogin(format2, format3);
        Integer integer3 = homeMapper.DayUsersLogin(format3, format4);
        Integer integer4 = homeMapper.DayUsersLogin(format4, format5);
        Integer integer5 = homeMapper.DayUsersLogin(format5, format6);
        Integer integer6 = homeMapper.DayUsersLogin(format6, format7);
        Integer integer7 = homeMapper.DayUsersLogin(format7, format8);

        int []a = {integer1,integer2,integer3,integer4,integer5,integer6,integer7};
        return a;
    }

    public LocalDate [] getSevenLogindate() {
        LocalDate now = LocalDate.now();
        LocalDate date6 = LocalDate.now().plusDays(-1);
        LocalDate date5 = LocalDate.now().plusDays(-2);
        LocalDate date4 = LocalDate.now().plusDays(-3);
        LocalDate date3 = LocalDate.now().plusDays(-4);
        LocalDate date2 = LocalDate.now().plusDays(-5);
        LocalDate date1 = LocalDate.now().plusDays(-6);

        LocalDate []datelist = {date1,date2,date3,date4,date5,date6,now};
        return datelist;
    }

    @Override
    public int getDateLogin() {
        LocalDate date1 = LocalDate.now();
        LocalDate date2 = LocalDate.now().plusDays(1);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String format1 = date1.format(formatter);
        String format2 = date2.format(formatter);
        String DayBegin = format1+" 00:00:00";
        String DayEnd = format2+" 00:00:00";
        int a = homeMapper.DayUsersLogin(DayBegin, DayEnd);
        return a;
    }

    @Override
    public int getDateLoginByUser(String username) {
        LocalDate date1 = LocalDate.now();
        LocalDate date2 = LocalDate.now().plusDays(1);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String format1 = date1.format(formatter);
        String format2 = date2.format(formatter);
        String DayBegin = format1+" 00:00:00";
        String DayEnd = format2+" 00:00:00";
        int a = homeMapper.DayUserLogin(username, DayBegin, DayEnd);
        return a;
    }

}
